/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.data;

import static com.vng.zing.survey.data.Base.serialize;
import com.vng.zing.survey.thrift.model.IDUserResponseItem;
import com.vng.zing.survey.thrift.model.Error;

import com.vng.zing.survey.thrift.model.IDUserResQuesNoRequiKey;

import java.io.IOException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import kyotocabinet.DB;

/**
 *
 * @author kienpd
 */
public class IDUserResponseData extends Base{
     private static final Lock createLock_ = new ReentrantLock();
    private static IDUserResponseData _instance = null;
    private static int buffer_size_profilevip = 0;
    private DB db;

    public IDUserResponseData() {
        db = new DB();
        if (!db.open("database/iduserresponse.kch", DB.OWRITER | DB.OCREATE)) {
            System.err.println("open error: " + db.error());
        }
    }

    public static IDUserResponseData getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new IDUserResponseData();
                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;
    }
    public Error setIDUserResponseItem(int idSurvey, IDUserResponseItem dUserResponseItem){
         try {
             byte[] key = serialize(idSurvey);
             byte[] value=serialize(dUserResponseItem);
             if(!db.set(key, value)){
             
                 return Error.ERR_SYSTEM;
             }
             return Error.SUCCESS;
         } catch (Exception ex) {
             Logger.getLogger(IDUserResponseData.class.getName()).log(Level.SEVERE, null, ex);
         }
           return Error.ERR_SYSTEM;
    }
    public IDUserResponseItem getIDUserResponseItem(int idSurvey){
         try {
             byte[] key = serialize(idSurvey);
             byte[]value= db.get(key);
             if(value== null){
                 return  null;
             }
             IDUserResponseItem dUserResponseItem = new IDUserResponseItem();
             dUserResponseItem = (IDUserResponseItem) deserialize(value);
             return dUserResponseItem;
             
         } catch (Exception ex) {
             Logger.getLogger(IDUserResponseData.class.getName()).log(Level.SEVERE, null, ex);
         }
          return  null;
    
    }
    public IDUserResponseItem getIDUserResQuesNoRequi(int idSurvey, short idQues){
         try {
             IDUserResQuesNoRequiKey dUserResQuseNoReQuriKey = new IDUserResQuesNoRequiKey(idSurvey, idQues);
             byte[] key = serialize(dUserResQuseNoReQuriKey);
             byte[]value= db.get(key);
             if(value== null){
                 return  null;
             }
             IDUserResponseItem dUserResponseItem = new IDUserResponseItem();
             dUserResponseItem = (IDUserResponseItem) deserialize(value);
             return dUserResponseItem;
             
         } catch (Exception ex) {
             Logger.getLogger(IDUserResponseData.class.getName()).log(Level.SEVERE, null, ex);
         }
          return  null;
    }
    public Error setIDUserResQuesNoRequi(int idSurvey,short idQues, IDUserResponseItem dUserResponseItem){
         try {
             IDUserResQuesNoRequiKey dUserResQuseNoReQuriKey = new IDUserResQuesNoRequiKey(idSurvey, idQues);
             byte[] key = serialize(dUserResQuseNoReQuriKey);
             byte[] value=serialize(dUserResponseItem);
             if(!db.set(key, value)){
             
                 return Error.ERR_SYSTEM;
             }
             return Error.SUCCESS;
         } catch (Exception ex) {
             Logger.getLogger(IDUserResponseData.class.getName()).log(Level.SEVERE, null, ex);
         }
           return Error.ERR_SYSTEM;
    }
    
}
